/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.gui.wizard.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;

import edu.drexel.sd0910.ece01.aqmon.gui.MainModel;
import edu.drexel.sd0910.ece01.aqmon.util.ImageUtils;

/**
 * Wizard - Node discovery panel. This panel is used to discover nodes by
 * sending out ND commands through the XBee device. It can also be used to Range
 * Test to see if you can "see" nodes.
 * 
 * @author Kyle O'Connor
 * 
 */
@SuppressWarnings("serial")
public class NodeDiscoveryPanel extends JPanel {
	
	private final static Logger log = Logger.getLogger(NodeDiscoveryPanel.class);

	private int MAX_NUM_NODES;

	// gui items
	private JPanel titlePanel;
	private JLabel textLabel;
	private JLabel headingLabel;
	private JLabel instructionsLabel;
	private JLabel[] nodeLabels;
	private JButton sendNDCommand;
	
	private static ImageIcon notFoundIcon = ImageUtils.createImageIcon("/not_found.png", "Node not found");
	private static ImageIcon foundIcon = ImageUtils.createImageIcon("/found.png", "Node not found");

	protected MainModel model;

	public NodeDiscoveryPanel(MainModel aModel) {
		this.model = aModel;
		MAX_NUM_NODES = model.getAddressMap().size();
		initComponents();
	}

	private void initComponents() {
		this.setLayout(new BorderLayout());

		JPanel centerPane = new JPanel(new BorderLayout());
		centerPane.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
		JPanel southPane = new JPanel();
		BoxLayout nodeBox = new BoxLayout(southPane, BoxLayout.Y_AXIS);
		southPane.setLayout(nodeBox);

		// title bar
		titlePanel = new JPanel(new BorderLayout());
		titlePanel.setBackground(Color.GRAY);
		textLabel = new JLabel();
		JSeparator separator = new JSeparator();
		textLabel.setBackground(Color.GRAY);
		textLabel.setFont(new Font(textLabel.getFont().getFontName(), Font.BOLD, 16));
		textLabel.setText("Step 2. Discover Connected Nodes (Range Test)");
		textLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		textLabel.setOpaque(true);
		titlePanel.add(textLabel, BorderLayout.CENTER);
		titlePanel.add(separator, BorderLayout.SOUTH);

		headingLabel = new JLabel("Nodes Discovered:");
		instructionsLabel = new JLabel(
				"Once you have discovered all nodes that are part of this deployment, click the 'Next' button.",
				JLabel.RIGHT);

		
		nodeLabels = new JLabel[MAX_NUM_NODES];
		for (int i = 0; i < nodeLabels.length; i++) {
			nodeLabels[i] = new JLabel("Node " + (i + 1), notFoundIcon, JLabel.CENTER);
			nodeLabels[i].setVerticalTextPosition(JLabel.CENTER);
			nodeLabels[i].setHorizontalTextPosition(JLabel.RIGHT);
			southPane.add(nodeLabels[i]);
		}
		
		sendNDCommand = new JButton("Discover Nodes");
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
		buttonPanel.add(sendNDCommand);

		centerPane.add(headingLabel, BorderLayout.NORTH);
		
		JPanel gridPane = new JPanel(new GridLayout(0,2));
		gridPane.add(southPane);
		gridPane.add(buttonPanel);
		centerPane.add(gridPane, BorderLayout.SOUTH);

		add(centerPane, BorderLayout.CENTER);
		add(titlePanel, BorderLayout.NORTH);
		add(instructionsLabel, BorderLayout.SOUTH);
	}
	
	public void setNodeFound(final String nodeID) {
		final int index = Integer.parseInt(nodeID.substring(nodeID.indexOf('N') + 1));
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				if (nodeLabels[index - 1].getIcon().equals(notFoundIcon)) {
					log.debug("Updating status to found for node "+nodeID);
					nodeLabels[index - 1].setIcon(foundIcon);
					//nodeLabels[index - 1].repaint();
				}
			}
		});
	}
	
	public void resetNodeFound() {
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				for (int i = 0; i < nodeLabels.length; i++) {
					nodeLabels[i].setIcon(notFoundIcon);
					//nodeLabels[i].repaint();
				}
			}
		});
	}

	public void addNodeDiscoveryButtonListener(ActionListener l) {
		sendNDCommand.addActionListener(l);
	}
	
	public void setButtonEnabled(final boolean state) {
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				sendNDCommand.setEnabled(state);
			}
		});
	}

}
